#!/bin/bash
USERNAME="$1"
OLD_PASSWD="$2"
NEW_PASSWD="$3"
UPDATE_LOGIN_KEYCHAIN="${4:-true}"

sudo /usr/sbin/sysadminctl -resetPasswordFor $USERNAME -newPassword "$NEW_PASSWD" -adminUser $USERNAME -adminPassword "$OLD_PASSWD"
sudo /usr/bin/python3 /Users/$USERNAME/bootstrap/kcpassword.py "$NEW_PASSWD"
sudo /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser "$USERNAME"

if [[ $UPDATE_LOGIN_KEYCHAIN == "true" ]]; then
    /usr/bin/security set-keychain-password -o "$OLD_PASSWD" -p "$NEW_PASSWD" /Users/$USERNAME/Library/Keychains/login.keychain
fi
